var express = require('express');
var router = express.Router();
var goodsDAO = require("../bin/db/goodsDAO.js");
var classDAO = require("../bin/db/classDAO.js");


var multer = require("multer");
var obj = [];
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './public/images')
  },
  filename: function (req, file, cb) {
    var suffix = file.mimetype.split("/")[1];
    var filename = file.fieldname + '-' + Date.now() + "." + suffix;
    req[file.fieldname] = filename;

    var img = req[file.fieldname];
    console.log(img);
    for (var i = 0; i < img.length; i++) {
      obj = obj + img[i];
    }
    cb(null, filename);
  }
})
var upload = multer({ storage: storage })


/* GET home page. */
router.get("/banner", function (req, res) {
  var msg = [
    { img: "/img/banner.jpg" }
  ]
  res.json({ err: 0, msg: msg })
})

router.get("/swiper", function (req, res) {
  var msg = [
    { img: "/img/1.jpg" },
    { img: "/img/2.jpg" },
    { img: "/img/3.jpg" },
    { img: "/img/4.jpg" },
    { img: "/img/5.jpg" },
    { img: "/img/6.jpg" },
    { img: "/img/7.jpg" },
    { img: "/img/8.jpg" }
  ]
  res.json({ err: 0, msg: msg })
})

router.get("/none", function (req, res) {
  var msg = [
    { title: "狗粮", img: "/img/9.png" },
    { title: "罐头", img: "/img/10.png" },
    { title: "零食", img: "/img/11.png" },
    { title: "保健", img: "/img/12.png" },
    { title: "医疗", img: "/img/13.png" },
    { title: "香波", img: "/img/14.png" },
    { title: "用品", img: "/img/15.png" },
    { title: "更多", img: "/img/16.png" }
  ]
  res.json({ err: 0, msg: msg })
})

router.get("/cat", function (req, res) {
  var msg = [
    { title: "干粮", img: "/img/17.png" },
    { title: "湿粮", img: "/img/18.png" },
    { title: "零食", img: "/img/19.png" },
    { title: "医疗", img: "/img/20.png" },
    { title: "保健", img: "/img/21.png" },
    { title: "香波", img: "/img/22.png" },
    { title: "用品", img: "/img/23.png" },
    { title: "更多", img: "/img/16.png" }
  ]
  res.json({ err: 0, msg: msg })
})

router.get("/pet", function (req, res) {
  var msg = [
    { title: "兔兔用品", img: "/img/24.png" },
    { title: "仓鼠用品", img: "/img/25.png" },
    { title: "龙猫用品", img: "/img/26.png" },
    { title: "荷兰猪用品", img: "/img/27.png" },
    { title: "更多", img: "/img/16.png" },
  ]
  res.json({ err: 0, msg: msg })
})

router.get("/fesh", function (req, res) {
  var msg = [
    { title: "水族箱", img: "/img/28.png" },
    { title: "鱼饲料", img: "/img/29.png" },
    { title: "过滤器", img: "/img/30.png" },
    { title: "药水", img: "/img/31.png" },
    { title: "装饰", img: "/img/32.png" },
    { title: "陆龟用品", img: "/img/33.png" },
    { title: "蜥蜴用品", img: "/img/34.png" },
    { title: "更多", img: "/img/16.png" }
  ]
  res.json({ err: 0, msg: msg })
})

router.get("/tuijian", function (req, res) {
  var msg = [
    { img: "/img/35.jpg" },
    { img: "/img/36.jpg" },
    { img: "/img/37.jpg" },
    { img: "/img/38.jpg" },
    { img: "/img/39.jpg" },
    { img: "/img/40.jpg" },
    { img: "/img/41.jpg" },
    { img: "/img/42.jpg" }
  ]
  res.json({ err: 0, msg: msg })
})

router.get("/kill", function (req, res) {
  var msg = [
    { title: "精品猫粮", price: "19", img: "/img/43.jpg" },
    { title: "精品猫粮", price: "19", img: "/img/44.jpg" },
    { title: "精品猫粮", price: "19", img: "/img/45.jpg" },
    { title: "精品猫粮", price: "19", img: "/img/46.jpg" },
    { title: "精品猫粮", price: "19", img: "/img/47.jpg" },
    { title: "精品猫粮", price: "19", img: "/img/48.jpg" },
    { title: "精品猫粮", price: "19", img: "/img/49.jpg" },
  ]
  res.json({ err: 0, msg: msg })
})

// 添加商品接口
router.post("/upload", upload.array("image"), function (req, res) {
  var n = new goodsDAO(req.body);
  n.save()
    .then(data => {
      var imobj = [];
      imobj = obj.split('i');
      // console.dir(imobj);
      for (i = imobj.length - 1; i > 0; i--) {
        data.img.push("/images/i" + imobj[i]);
      }
      obj = [];
      data.save(function (err) {
        if (!err) {
          res.json({ err: 0, msg: '发布成功' });

        }
      });
    });
})


router.post("/class", function (req, res) {
  classDAO.findOne({ bigtype: req.body.bigclasses })
    .then(data => {
      if (!data) {
        var p = new classDAO({
          bigtype: req.body.bigclasses,
          littletype: req.body.littleclasses
        });
        p.save()
      } else {
        if (data.littletype.indexOf(req.body.littleclasses) == -1) {
          data.littletype.push(req.body.littleclasses)
          data.save()
        }
      }
    })


})

router.get("/typeclass", function (req, res) {
  classDAO.find()
    .then(data => {
      res.json({ data })
    })
})

router.post("/dog", function (req, res) {
  classDAO.findOne({ bigtype: req.body.title })
    .then(data => {
      res.json({ data })
    })
})

router.post("/doglist", function (req, res) {
  goodsDAO.find({ littleclasses: req.body.class })
  .skip(0)
  .limit(8)
    .then(data => {
      res.json({ data })
    })
})

router.post("/catlist", function (req, res) {
  goodsDAO.find({ littleclasses: req.body.class }).sort({ "price": 1 })
    .then(data => {
      res.json({ data })
    })
})

router.post("/catlist2", function (req, res) {
  console.log(req.body)
  goodsDAO.find({ littleclasses: req.body.class })
  .sort({ "price": -1 })
  .skip(req.body.i)
  .limit(5)
  .then(data => {
      res.json({ data })
    })
})


router.post("/catlist3", function (req, res) {
  // console.log(req.body)
  goodsDAO.find({ littleclasses: req.body.class }).sort({ "price": -1 })

    .then(data => {
      res.json({ data })
    })
})

router.post("/catlist4", function (req, res) {
  console.log(req.body.i)
  goodsDAO.find({ littleclasses: req.body.class }).sort({ "price": -1 })
  .skip(req.body.i)
  .limit(5)

    .then(data => {
      res.json({ data })
    })
})
router.post("/home", function (req, res) {
  var p = req.body.name
  goodsDAO.find({ name: { $regex: p } }).sort({ "price": 1 })
    .then(data => {
      res.json({ data })
    })
})

router.post("/home2", function (req, res) {
  var p = req.body.name
  goodsDAO.find({ name: { $regex: p } }).sort({ "price": -1 })
    .then(data => {
      res.json({ data })
    })
})

router.post("/twodetail", function (req, res) {
  var p = req.body.name
  goodsDAO.find({ name: { $regex: p }, bigclasses: req.body.bigclasses }).sort({ "price": 1 })
    .then(data => {
      res.json({ data })
    })
})

router.post("/twodetail2", function (req, res) {
  var p = req.body.name
  goodsDAO.find({ name: { $regex: p }, bigclasses: req.body.bigclasses }).sort({ "price": -1 })
    .then(data => {
      res.json({ data })
    })
})


router.post("/goodsdetail", function (req, res) {
  goodsDAO.findOne({ _id: req.body._id })
    .then(data => {
      res.json({ data })
    })
})

router.post("/all",function(req,res){
  goodsDAO.find()
  .then(data=>{
    res.json({data})
  })
})

router.post("/key", function (req, res) {
  console.log(req.body)
  var p = req.body.params.word
  goodsDAO.find({ name: { $regex: p } })
    .then(data => {
      res.json({ err: 0, data })
    })
})



module.exports = router;
